//
//  HWAssetsProtocol.swift
//  HWPromoteProjectAPP
//
//  Created by 王昌健 on 2022/1/27.
//  Copyright © 2022 王昌健. All rights reserved.
//

import UIKit

class HWAssetsProtocol: NSObject {
//    var centerImageArr = ["purse","promoteUser","discount","promoteGame"]
//    var centerTitleArr = ["累计赚取","推广用户","折扣代充","推广游戏"]
    var centerImageArr = ["purse","promoteUser","promoteGame"]
    var centerTitleArr = ["累计赚取","推广用户","推广游戏"]
    var assetsModel:HWAssetsModel?
    var models:[HWAssetsIncomeDetailModel]?
    
    var promoteNoticeButtonClick:(()->())?
    var promoteQRCodeButtonClick:(()->())?
    var divideDetailButtonClick:(()->())?
    var didClickRow:((_:IndexPath)->())?
    var applyButtonClick:(()->())?
}

extension HWAssetsProtocol:UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 1 {
            self.didClickRow?(indexPath)
        }
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let topHeader = HWAssetsTopHeader.headerOrFooterWithTableView(tableView: tableView)
        let bottomHeader = HWAssetsIncomeHeader.headerOrFooterWithTableView(tableView: tableView)
        if section == 0 {
            topHeader.assetsModel = assetsModel
            topHeader.applyButtonClick = {
                self.applyButtonClick?()
            }
            return topHeader
        } else if section == 2 {
            return bottomHeader
        }else {
            return UIView()
        }
        
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return 244*widthScale
        }else if section == 2 {
            return 58*widthScale
        }else {
            return 0
        }
        
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return 1
        }else if section == 1 {
            return centerImageArr.count
        }else {
            return (models?.count ?? 0) + 1
        }
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let topCell = HWAssetsTopCell.cellWithTableView(tableView: tableView)
        let centerCell = HWAssetsCenterCell.cellWithTableView(tableView: tableView)
        let incomeCell = HWAssetsIncomeCell.cellWithTableView(tableView: tableView)
        if indexPath.section == 0 {
            topCell.promoteNoticeButtonClick = {
                self.promoteNoticeButtonClick?()
            }
            topCell.promoteQRCodeButtonClick = {
                self.promoteQRCodeButtonClick?()
            }
            topCell.divideDetailButtonClick = {
                self.divideDetailButtonClick?()
            }
            return topCell
        }else if indexPath.section == 1 {
            centerCell.leftImageView.image = UIImage(named: centerImageArr[indexPath.row])
            centerCell.titleLabel.text = centerTitleArr[indexPath.row]
            centerCell.assetsModel = assetsModel
            centerCell.indexPath = indexPath
            return centerCell
        } else{
            incomeCell.indexPath = indexPath
            if indexPath.row == 0 {
                incomeCell.nameLabel.text = "用户名"
                incomeCell.priceLabel.text = "充值金额"
                incomeCell.timeLabel.text = "充值时间"
                incomeCell.divideLabel.text = "佣金分成"
            }else {
                incomeCell.model = models?[indexPath.row-1]
            }
            
            return incomeCell
        }
    }
    
    
}
